package com.application;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.*;
import util.core.Atom;
import util.input.InputFixer;

import java.util.ArrayList;

import static util.core.Periodic.*;

public class PeriodicTable extends Activity {

    private View.OnClickListener mainListener = new View.OnClickListener() {
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.periodic_get) {
                String searchString = ((EditText) findViewById(R.id.periodic_text)).getText().toString();
                Atom searchAtom = elements.get(searchString);
                if (searchAtom == null) {
                    if (searchString.length() > 2)
                        searchAtom = findByName(searchString);
                    else {
                        InputFixer c = new InputFixer(searchString);
                        c.fix1();
                        searchAtom = elements.get(c.getResult());
                        if (searchAtom != null) {
                            ((TextView) findViewById(R.id.periodic_result)).setText(Html.fromHtml(searchAtom.info()));
                            ((EditText) findViewById(R.id.periodic_text)).setText(c.getResult());
                        }
                    }

                }
                if (searchAtom != null)
                    ((TextView) findViewById(R.id.periodic_result)).setText(Html.fromHtml(searchAtom.info()));

            } else if (id == R.id.periodic_select) {
                populate();
            }
        }
    };
    private Spinner attribute_spinner;
    private String attribute, current_text;
    private boolean list;
    private AdapterView.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            attribute = attribute_spinner.getSelectedItem().toString();
        }


        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.periodic_info);
        attribute = "name";
        init();
    }

    private void init() {
        list = false;
        attribute_spinner = (Spinner) findViewById(R.id.periodic_spinner);
        ArrayAdapter<? extends CharSequence> c = ArrayAdapter.createFromResource(this, R.array.atom_attributes, android.R.layout.simple_spinner_item);
        c.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        findViewById(R.id.periodic_get).setOnClickListener(mainListener);
        findViewById(R.id.periodic_select).setOnClickListener(mainListener);
        attribute_spinner.setAdapter(c);
        attribute_spinner.setOnItemSelectedListener(spinnerListener);
        if (attribute.equals("Name"))
            attribute_spinner.setSelection(0);
        else if (attribute.equals("Symbol"))
            attribute_spinner.setSelection(1);
        else if (attribute.equals("Number"))
            attribute_spinner.setSelection(2);
        else if (attribute.equals("Mass"))
            attribute_spinner.setSelection(3);
    }

    private void populate() {
        ArrayList toPopulate = null;
        if (attribute.equals("Name")) {
            toPopulate = elementNames;
        } else if (attribute.equals("Symbol")) {
            toPopulate = elementSymbols;
        } else if (attribute.equals("Number")) {
            toPopulate = numbers;
        } else if (attribute.equals("Mass")) {
            toPopulate = masses;
        }
        final CharSequence[] temp = new CharSequence[elements.size() + 1];

        if (toPopulate != null) {
            for (int i = 0; i < toPopulate.size(); i++) {
                temp[i] = toPopulate.get(i).toString();
            }
            temp[toPopulate.size()] = "Cancel";
        }
        current_text = ((EditText) findViewById(R.id.periodic_text)).getText().toString();
        ListView listView = new ListView(getApplicationContext());
        listView.setAdapter(new CustomAdapter<CharSequence>(getApplicationContext(), android.R.layout.simple_list_item_1, temp));
        setContentView(listView);
        list = true;
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                list = false;
                setContentView(R.layout.periodic_info);
                init();
                int spinner_position = attribute_spinner.getSelectedItemPosition();
                if (position != elements.size()) {
                    if (spinner_position == 0) {
                        ((EditText) findViewById(R.id.periodic_text)).setText(elementNames.get(position));
                    } else if (spinner_position == 1) {
                        ((EditText) findViewById(R.id.periodic_text)).setText(elementSymbols.get(position));
                    } else if (spinner_position == 2) {
                        ((EditText) findViewById(R.id.periodic_text)).setText(findByNumber(Integer.parseInt(temp[position].toString())).getName());
                    } else if (spinner_position == 3) {
                        ((EditText) findViewById(R.id.periodic_text)).setText(findByMass(Double.parseDouble(temp[position].toString())).getName());
                    }
                }
            }
        });
        listView.setFastScrollEnabled(true);
    }

    public void onBackPressed() {
        if (list) {
            setContentView(R.layout.periodic_info);
            init();
            ((EditText) findViewById(R.id.periodic_text)).setText(current_text);
        } else
            finish();
    }


}